//
//  Goblin.m
//  DefenseSummoner
//
//  Created by vupham on 5/28/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Goblin.h"
#import "GameLayer.h"
#import "iCoreGUIController.h"

@implementation Goblin
-(id)initGoblinWith:(PlayScreen*)_playScreen withRoad:(NSMutableArray*)_road{
    self=[super initEnemyWith:(GameLayer *)_playScreen];
    armor=0;
    HP=100; //blood for testing
    speed=0.75f;
    spriteBody = [[CCSprite alloc]initWithSpriteFrameName:@"Goblin_Move_01.png"];
    road=_road;
    damage = 10;
    moneyOnDie=20;
    nextHeadingPoint=1;
    
    [self drawBloodMeterWithLength:spriteBody.contentSize.width/2 + 10 width:3 position:ccp(spriteBody.contentSize.width/2, spriteBody.contentSize.height)];
    
    return self;
}

-(void)animationMoveLeft{
    //NSLog(@"Running");
    [super animationMoveLeft];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Goblin_Move" withCount:8 delay:0.1f];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
}

-(void)animationMoveUp{
    [super animationMoveUp];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Goblin_Move_Up" withCount:12 delay:0.1];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
}

-(void)animationMoveDown{
    [super animationMoveDown];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Goblin_Move_Down" withCount:12 delay:0.1];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]]];
}

-(void)animationBattle{
    [super animationBattle];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Goblin_Attack" withCount:9 delay:0.1f];
    CCCallFunc *func=[CCCallFunc actionWithTarget:self selector:@selector(battle)];
    [spriteBody runAction:[CCRepeatForever actionWithAction:[CCSequence actions:[CCAnimate actionWithAnimation:animation],[CCDelayTime actionWithDuration:0.5],func, nil]]];
}

-(void)animationDie{
    [super animationDie];
    CCAnimation *animation = [self loadAnimationFromCacheByName:@"Goblin_Die" withCount:4 delay:0.1f];
    CCCallFunc *dieCallBack = [CCCallFunc actionWithTarget:self selector:@selector(dieCallBack)];
    [spriteBody runAction:[CCSequence actions:[CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO],[CCDelayTime actionWithDuration:0.75f], dieCallBack, nil]];
    
    if(!handler.mainGUIController.isMuteSound) {
        [handler.mainGUIController playEffect:AUDIO_GOBLIN_DIE];
    }
}

-(void)dealloc {
    NSLog(@"Goblin dealoc");
    [self removeAnimationInFrameCache:@"Goblin_Move"];
    [self removeAnimationInFrameCache:@"Goblin_Move_Up"];
    [self removeAnimationInFrameCache:@"Goblin_Move_Down"];
    [self removeAnimationInFrameCache:@"Goblin_Attack"];
    [self removeAnimationInFrameCache:@"Goblin_Die"];
    [super dealloc];
}

@end
